package com.quad.innovators.salesease.common.result;

import com.quad.innovators.salesease.enums.ResultCode;
import com.quad.innovators.salesease.exception.BusinessException;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Result<T> {
    @Schema(description = "业务状态码 200：成功  其他: 失败")
    private int code;
    @Schema(description = "提示信息")
    private String message;
    @Schema(description = "返回数据")
    private T data;

    public static <E> Result<E> success(E data) {
        return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);
    }

    public static <E> Result<E> success() {
        return new Result<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);
    }

    public static <E> Result<E> error(String message) {
        return new Result<>(ResultCode.INTERNAL_SERVER_ERROR.getCode(), ResultCode.INTERNAL_SERVER_ERROR.getMsg(), null);
    }

    public static <T> Result<T> judge(boolean status) {
        if (status) {
            return success();
        } else {
            return otherErr(ResultCode.INTERNAL_SERVER_ERROR);
        }
    }

    //自定义异常返回的结果
    public static <T> Result<T> businessErr(BusinessException e) {
        return new Result<>(e.getErrorCode(), e.getErrorMsg(), null);
    }

    //其他异常处理方法返回的结果
    public static <T> Result<T> otherErr(ResultCode resultCode){
        return new Result<>(resultCode.getCode(), resultCode.getMsg(), null);
    }

    //其他异常处理方法返回的结果
    public static <T> Result<T> otherErr(ResultCode resultCode, String message){
        return new Result<>(resultCode.getCode(),message, null);
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
